import sys
from optparse import OptionParser
from datetime import datetime,timedelta
import time
import logging
import os

try:
    import json
except ImportError:
    import simplejson as json

from iso8601 import iso8601

import pydelicious

import locale 
try:
        preferredencoding = locale.getpreferredencoding()
except:
        preferredencoding = None

#handle situations where we get blank 
#strings for locale.getpreferredencoding() 
if preferredencoding == None or  preferredencoding == '':
    preferredencoding = 'iso-8859-1'


def fromdt(fromdt_file,value=None):
    if value == None:
        f = open(fromdt_file)
        fromdt = f.read()
        f.close()
        return fromdt
    else:
        f = open(fromdt_file,'w')
        f.write(value)
        f.close()

def main():
    usage = "usage: %prog [options] config"
    parser = OptionParser(usage)

    parser = OptionParser()
    parser.add_option("-u", "--username", dest="username",help="Deliciuos username")
    parser.add_option("-p", "--password", dest="password", help="Delicious password")
    parser.add_option("-t", "--fromdt", dest="fromdt", help="Get Delicious posts \
since this date. Format YYYY-MM-DDThh:mm:ssZ. If not passed will look for \
the value from the fromdt_file. Will not run without this value.")
    parser.add_option("-c", "--fromdt_file",dest="fromdt_file",default="./FROMDT",
    help="Path to file that we will use to store the last updated values (fromdt).\
Defaults to a file called FROMDT in the current directory.")
    parser.add_option("-o", "--output_dir",dest="output_dir",default="./delicious_json_posts",
    help="Path to dir that we will use to store the downloaded delicious posts. Stores it \
in json format. Defauts to ./delicious_json_posts. If dir doesn't exist, will \
attempt to create it.")
    parser.add_option("-l","--log",dest="log", default=None, help="Defaults to\
STDOUT")
    parser.add_option("-d","--debug", dest="debug", action='store_true', default=False)
 
    (options, args) = parser.parse_args()

    if options.log != None:
        logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)-8s %(message)s',
                    filename=log_file,
                    filemode='a+')
    else:
        logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)-8s %(message)s')

    if (options.username is None) or (options.password is None):
        logging.error("Delicious username or password not provided.")
        sys.exit(1)

    if not os.path.exists(options.output_dir):
        try:
            os.mkdir(options.output_dir)
        except OSError,e:
            logging.exception('Failed to make output_dir %s'%(options.output_dir))
            sys.exit(1)

    if options.fromdt is None:
        try:
            last_updated = iso8601.parse_date(fromdt(options.fromdt_file))
        except IOError,e:
            logging.exception("Failed to read the fromdt value from the path : %s. If \
            this is your first run use the --fromdt option to pass in a date \
            (YYYY-MM-DDThh:mm:ssZ) to start from."%(options.fromdt_file))
            sys.exit(1)
        except:
            logging.exception("Probably problem parsing stored last_modified (fromdt) value from file.")
            sys.exit(1)
    else:
        try:
            last_updated = iso8601.parse_date(options.fromdt)
        except:
            logging.exception("Problem parsing passed in --fromdt parameter. Must be in format YYY-MM-DDTHH:MM:SS")
            sys.exit(1)

    if options.debug:
        pydelicious.DEBUG =5
        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)

    

    conn = pydelicious.DeliciousAPI(options.username,options.password,codec=preferredencoding)
    
    last_modified = datetime(*conn.posts_update()['update']['time'][:-3])
    #set UTC=0 as the timezone - is this right ?
    last_modified = last_modified.replace(tzinfo=iso8601.Utc())
    
    logging.debug("Delicious.com - last update %s"%(last_modified))
    logging.debug("Local last update - last update %s"%(last_updated))

    if last_modified > last_updated:
        #we add a second so that we dont end up redownloading the post with
        #the last_updated entry 
        last_updated = last_updated + timedelta(seconds=1)
        data = conn.posts_all(fromdt=last_updated.strftime(pydelicious.ISO_8601_DATETIME))
        logging.debug("Retrieved %s posts"%(data['total']))
        for post in data['posts']:
            f = open(
                os.path.join(options.output_dir,'%s_%s.json'%(post['time'],post['hash'])),
                'w') 
            f.write(json.dumps(post))
            f.close()
    fromdt(options.fromdt_file,last_modified.strftime(pydelicious.ISO_8601_DATETIME))
    
if __name__ == "__main__":
    main()
